package com.cda.pes.service.impl;

import com.cda.pes.mapper.ProductMapper;
import com.cda.pes.pojo.Product;
import com.cda.pes.service.IProductService;
import com.cda.pes.service.ex.DeleteException;
import com.cda.pes.service.ex.InsertException;
import com.cda.pes.service.ex.ProductNotFoundException;
import com.cda.pes.service.ex.UpdateException;
import com.cda.pes.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/** 处理商品数据的业务层实现类 */
@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<Product> findHotList() {
        List<Product> list = productMapper.findHotList();
        for (Product product : list) {
            product.setPriority(null);
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedUser(null);
            product.setModifiedTime(null);
        }
        return list;
    }

    @Override
    public Product findById(Integer id) {
        // 根据参数id调用私有方法执行查询，获取商品数据
        Product product = productMapper.findById(id);
        // 判断查询结果是否为null
        if (product == null) {
            // 是：抛出ProductNotFoundException
            throw new ProductNotFoundException("尝试访问的商品数据不存在");
        }
        // 将查询结果中的部分属性设置为null
        product.setPriority(null);
        product.setCreatedUser(null);
        product.setCreatedTime(null);
        product.setModifiedUser(null);
        product.setModifiedTime(null);
        // 返回查询结果
        return product;
    }

    @Override
    public List<Product> findLastedList() {
        List<Product> list = productMapper.findLastedList();
        for (Product product : list) {
            product.setPriority(null);
            product.setCreatedUser(null);
            product.setCreatedTime(null);
            product.setModifiedUser(null);
            product.setModifiedTime(null);
        }
        return list;
    }

    @Override
    public Integer addProduct(Product product) {

        Product result = productMapper.findById(product.getId());
        if (result != null){
            System.out.println(result);
            throw new InsertException("插入商品时产生了未知的异常！");
        }
        Integer rows = productMapper.addProduct(product);
        System.out.println(rows);
        return rows;
    }

    @Override
    public void updateProduct(Product product) {
        product.setModifiedUser("管理员");
        product.setModifiedTime(new Date());
        productMapper.updateProductById(product);
    }

    //    , Integer categoryId,
//    String itemType, String title, String sellPoint,
//    Long price, Integer num, String image, Integer status,
//    Integer priority, Date modifiedTime, String modifiedUser
//    @Override
//    public void updateProduct(Product product, Integer id) {
//        Product result = productMapper.findById(id);
//        if (result == null || result.getStatus() == 0){
//            throw new ProductNotFoundException("商品数据不存在");
//        }
//        product.setId(id);
//        product.setModifiedUser("管理员");
//        product.setModifiedTime(new Date());
//
//        Integer rows = productMapper.updateProductById(product);
//        if (rows != 1){
//            throw new UpdateException("更新商品数据时产生未知异常");
//        }
//    }

    @Override
    public void deleteProduct(Integer id) {
        Product result = productMapper.findById(id);
        System.out.println(result);
        Integer rows = productMapper.deleteProduct(id);
        if (rows != 1){
            throw new DeleteException("删除商品时产生未知的异常");
        }
    }

    @Override
    public List<Product> findAll() {
        List<Product> list = productMapper.findAll();
        return list;
    }

    @Override
    public List<Product> findAllByType(String typename) {
        List<Product> list = productMapper.findAllByType(typename);
        return list;
    }

}
